package com.promfy.security;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

import android.content.Context;
import android.net.ConnectivityManager;
import android.telephony.TelephonyManager;
import android.test.AndroidTestCase;
import android.util.Log;

import com.promfy.util.FileUtil;
import com.promfy.util.TcpUdpUtil;
import com.promfy.util.ZipUtil;

public class TestUtil extends AndroidTestCase {
	private static final String TAG = "test";

	private String password = "123456";
	private String alias = "www.zlex.org";
	private String certificatePath = "d:/zlex.cer";
	private String keyStorePath = "d:/zlex.keystore";

	public void test() throws Exception {
		System.err.println("公钥加密——私钥解密");
		String inputStr = "Ceritifcate";
		byte[] data = inputStr.getBytes();

		byte[] encrypt = CertificateCoder.encryptByPublicKey(data, certificatePath);

		byte[] decrypt = CertificateCoder.decryptByPrivateKey(encrypt, keyStorePath, alias, password);
		String outputStr = new String(decrypt);

		System.err.println("加密前: " + inputStr + "\n\r" + "解密后: " + outputStr);

		// 验证数据一致
		// assertArrayEquals(data, decrypt);

		// 验证证书有效
		assertTrue(CertificateCoder.verifyCertificate(certificatePath));

	}

	public void testSign() throws Exception {
		System.err.println("私钥加密——公钥解密");

		String inputStr = "sign";
		byte[] data = inputStr.getBytes();

		byte[] encodedData = CertificateCoder.encryptByPrivateKey(data, keyStorePath, alias, password);

		byte[] decodedData = CertificateCoder.decryptByPublicKey(encodedData, certificatePath);

		String outputStr = new String(decodedData);
		System.err.println("加密前: " + inputStr + "\n\r" + "解密后: " + outputStr);
		// assertEquals(inputStr, outputStr);

		System.err.println("私钥签名——公钥验证签名");
		// 产生签名
		String sign = CertificateCoder.sign(encodedData, keyStorePath, alias, password);
		System.err.println("签名:\r" + sign);

		// 验证签名
		boolean status = CertificateCoder.verify(encodedData, sign, certificatePath);
		System.err.println("状态:\r" + status);
		// assertTrue(status);

	}
	
	
	
	/**
	 * 测试对称性加密DES	
	 * @throws Exception
	 */
	public void testDES() throws Exception {
		String inputStr = "DES";
		String key = DESCoder.initKey();
		System.err.println("原文:\t" + inputStr);

		System.err.println("密钥:\t" + key);

		byte[] inputData = inputStr.getBytes();
		inputData = DESCoder.encrypt(inputData, key);

		System.err.println("加密后:\t" + DESCoder.encryptBASE64(inputData));

		byte[] outputData = DESCoder.decrypt(inputData, key);
		String outputStr = new String(outputData);

		System.err.println("解密后:\t" + outputStr);

		assertEquals(inputStr, outputStr);
	}
	
	/**
	 * 测试基于密码加密算法PBE(对称性加密)
	 * @throws Exception
	 */
	public void testPBE() throws Exception {
		String inputStr = "abc";
		System.err.println("原文: " + inputStr);
		byte[] input = inputStr.getBytes();

		String pwd = "efg";
		System.err.println("密码: " + pwd);

		byte[] salt = PBECoder.initSalt();

		byte[] data = PBECoder.encrypt(input, pwd, salt);

		System.err.println("加密后: " + PBECoder.encryptBASE64(data));

		byte[] output = PBECoder.decrypt(data, pwd, salt);
		String outputStr = new String(output);

		System.err.println("解密后: " + outputStr);
		assertEquals(inputStr, outputStr);
	}

		/**
		 * for RSA
		 */
		private String publicKey;
		private String privateKey;
		public void setUp() throws Exception {
			Map<String, Object> keyMap = RSACoder.initKey();

			publicKey = RSACoder.getPublicKey(keyMap);
			privateKey = RSACoder.getPrivateKey(keyMap);
			System.err.println("公钥: \n\r" + publicKey);
			System.err.println("私钥： \n\r" + privateKey);
		}

		/**
		 * 测试RSA加密算法
		 * @throws Exception
		 */
		public void testRSA() throws Exception {
			setUp(); 
			System.err.println("公钥加密——私钥解密");
			String inputStr = "abc";
			byte[] data = inputStr.getBytes();

			byte[] encodedData = RSACoder.encryptByPublicKey(data, publicKey);

			byte[] decodedData = RSACoder.decryptByPrivateKey(encodedData,
					privateKey);

			String outputStr = new String(decodedData);
			System.err.println("加密前: " + inputStr + "\n\r" + "解密后: " + outputStr);
			assertEquals(inputStr, outputStr);

		}
		
		/**
		 * 测试RSA加密算法
		 * @throws Exception
		 */
		public void testRSASign() throws Exception {
			setUp();
			System.err.println("私钥加密——公钥解密");
			String inputStr = "sign";
			byte[] data = inputStr.getBytes();

			byte[] encodedData = RSACoder.encryptByPrivateKey(data, privateKey);

			byte[] decodedData = RSACoder
					.decryptByPublicKey(encodedData, publicKey);

			String outputStr = new String(decodedData);
			System.err.println("加密前: " + inputStr + "\n\r" + "解密后: " + outputStr);
			assertEquals(inputStr, outputStr);

			System.err.println("私钥签名——公钥验证签名");
			// 产生签名
			String sign = RSACoder.sign(encodedData, privateKey);
			System.err.println("签名:\r" + sign);

			// 验证签名
			boolean status = RSACoder.verify(encodedData, publicKey, sign);
			System.err.println("状态:\r" + status);
			assertTrue(status);

		}

	

	
	
	
	
		/**
		 * 测试DH加密算法,(基于密钥一致协议的加密算法)
		 * @throws Exception
		 */
			public void testDH() throws Exception {
				// 生成甲方密钥对儿
				Map<String, Object> aKeyMap = DHCoder.initKey();
				String aPublicKey = DHCoder.getPublicKey(aKeyMap);
				String aPrivateKey = DHCoder.getPrivateKey(aKeyMap);

				System.err.println("甲方公钥:\r" + aPublicKey);
				System.err.println("甲方私钥:\r" + aPrivateKey);
				
				// 由甲方公钥产生本地密钥对儿
				Map<String, Object> bKeyMap = DHCoder.initKey(aPublicKey);
				String bPublicKey = DHCoder.getPublicKey(bKeyMap);
				String bPrivateKey = DHCoder.getPrivateKey(bKeyMap);
				
				System.err.println("乙方公钥:\r" + bPublicKey);
				System.err.println("乙方私钥:\r" + bPrivateKey);
				
				String aInput = "abc ";
				System.err.println("原文: " + aInput);

				// 由甲方公钥，乙方私钥构建密文
				byte[] aCode = DHCoder.encrypt(aInput.getBytes(), aPublicKey,
						bPrivateKey);

				// 由乙方公钥，甲方私钥解密
				byte[] aDecode = DHCoder.decrypt(aCode, bPublicKey, aPrivateKey);
				String aOutput = (new String(aDecode));

				System.err.println("解密: " + aOutput);

				assertEquals(aInput, aOutput);

				System.err.println(" ===============反过来加密解密================== ");
				String bInput = "def ";
				System.err.println("原文: " + bInput);

				// 由乙方公钥，甲方私钥构建密文
				byte[] bCode = DHCoder.encrypt(bInput.getBytes(), bPublicKey,
						aPrivateKey);

				// 由甲方公钥，乙方私钥解密
				byte[] bDecode = DHCoder.decrypt(bCode, aPublicKey, bPrivateKey);
				String bOutput = (new String(bDecode));

				System.err.println("解密: " + bOutput);

				assertEquals(bInput, bOutput);
			}

			/**
			 * 测试DSA加密算法,(DSA数字签名，非对称加密)
			 * @throws Exception
			 */
				public void testDSA() throws Exception {
					String inputStr = "abc";
					byte[] data = inputStr.getBytes();

					// 构建密钥
					Map<String, Object> keyMap = DSACoder.initKey();

					// 获得密钥
					String publicKey = DSACoder.getPublicKey(keyMap);
					String privateKey = DSACoder.getPrivateKey(keyMap);

					System.err.println("公钥:\r" + publicKey);
					System.err.println("私钥:\r" + privateKey);

					// 产生签名
					String sign = DSACoder.sign(data, privateKey);
					System.err.println("签名:\r" + sign);

					// 验证签名
					boolean status = DSACoder.verify(data, publicKey, sign);
					System.err.println("状态:\r" + status);
					assertTrue(status);

				}


}
